﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace S4Store.Controllers
{
    public class UploadController : Controller
    {

        public ActionResult Index()
        {
            try
            {
                var file = Request.Files[0];
                string fileexten = System.IO.Path.GetExtension(file.FileName).ToLower();
                checkextend(fileexten);
                string filename = System.IO.Path.GetFileNameWithoutExtension(file.FileName);
                string realname = System.Guid.NewGuid().ToString("D");
                string reafilename = realname + fileexten;
                string responseurl = "/UploadFile/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + reafilename;
                checkfolder(Server.MapPath("/UploadFile/" + DateTime.Now.ToString("yyyy-MM-dd") + "/"));
                file.SaveAs(Server.MapPath(responseurl));
                return new JsonResult
                {
                    Data = new
                    {
                        err="",
                        msg = new
                        {
                            url = responseurl,
                            localname = reafilename,
                            id=1
                        }
                    }
                };
            }
            catch(Exception exp)
            {
                return new JsonResult
                {
                    Data = new
                    {
                        err=exp.ToString()
                    }
                };
            }
        }
        private void checkfolder(string path)
        {
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
        }
        private void checkextend(string extend)
        {
            if (!extend.Contains("doc")
                && !extend.Contains("xls")
                && !extend.Contains("xlsx")
                && !extend.Contains("rar")
                && !extend.Contains("zip")
                && !extend.Contains("txt")
                && !extend.Contains("ppt")
                && !extend.Contains("jpg")
                && !extend.Contains("png")
                && !extend.Contains("gif")
                && !extend.Contains("bmp")
                && !extend.Contains("swf")
                && !extend.Contains("avi"))
            {
                throw new Exception("不允许上传此类文件");
            }
        }


        public ActionResult Xls()
        {
            try
            {
                var file = Request.Files[0];
                string fileexten = System.IO.Path.GetExtension(file.FileName).ToLower();
                if (fileexten != ".xls" && fileexten != ".xlsx")
                {
                    throw new Exception("只能上传EXCEL文件");
                }
                string filename = System.IO.Path.GetFileNameWithoutExtension(file.FileName);
                string realname = System.Guid.NewGuid().ToString("D");
                string reafilename = realname + fileexten;
                string responseurl = "/UploadFile/" + DateTime.Now.ToString("yyyy-MM-dd") + "/" + reafilename;
                checkfolder(Server.MapPath("/UploadFile/" + DateTime.Now.ToString("yyyy-MM-dd") + "/"));
                file.SaveAs(Server.MapPath(responseurl));
                return new JsonResult
                {
                    Data = new
                    {
                            url = responseurl,
                            filename =System.IO.Path.GetFileName(file.FileName)
                    }
                };
            }
            catch (Exception exp)
            {
                return new JsonResult
                {
                    Data = new
                    {
                        err = exp.ToString()
                    }
                };
            }
        }

    }

    
}
